
package com.afd.ozblip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.afd.ozblip.share.ShareConfig;
import com.afd.ozblip.share.TokenStore;
import com.afd.ozblip.share.WeiBoUserEntity;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMInfoAgent;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SocializeClientListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMDataListener;
import com.umeng.socialize.exception.SocializeException;

import java.util.Map;

public class GamAccountActivity extends BaseActivity {

    /****************** 视图变量 **************************/
    private Button twitterBtn;
    private Button facebookBtn;
    private Button sinaBtn;
    private Button qqBtn;
    private TextView twitterTv;
    private TextView facebookTv;
    private TextView sinaTv;
    private TextView qqTv;
    // sdk controller
    private UMSocialService mController = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gam_account_bind);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
    }

    /**
     * 寻找视图
     */
    public void findViews() {
        // this.twitterBtn = (Button)
        // findViewById(R.id.btn_bind_account_twitter);
        // this.facebookBtn = (Button)
        // findViewById(R.id.btn_bind_account_facebook);
        this.sinaBtn = (Button) findViewById(R.id.btn_bind_account_sina);
        this.qqBtn = (Button) findViewById(R.id.btn_bind_account_qq);
        this.sinaTv = (TextView) findViewById(R.id.tv_bind_account_sina);
        this.qqTv = (TextView) findViewById(R.id.tv_bind_account_qq);
        mController = UMServiceFactory.getUMSocialService(ShareConfig.DESCRIPTOR,
                RequestType.SOCIAL);

        // 获取新浪的用户信息
        WeiBoUserEntity sinaInfo = TokenStore.fetchUser(mContext, ShareConfig.SHARE_WEIBO_SINA);
        if (sinaInfo != null) {
            this.sinaTv.setText(sinaInfo.getUserName());
        }
        // 获取腾讯的用户信息
        WeiBoUserEntity qqInfo = TokenStore.fetchUser(mContext, ShareConfig.SHARE_WEIBO_QQ);
        if (qqInfo != null) {
            this.qqTv.setText(qqInfo.getUserName());
        }
        // 判断新浪是否授权
        if (UMInfoAgent.isOauthed(mContext, SHARE_MEDIA.SINA)) {
            sinaBtn.setText(R.string.unbind);
            sinaBtn.setBackgroundResource(R.drawable.unbind);
        } else {
            sinaBtn.setText(R.string.bind);
            sinaBtn.setBackgroundResource(R.drawable.bind);
        }
        // 判断腾讯是否授权
        if (UMInfoAgent.isOauthed(mContext, SHARE_MEDIA.TENCENT)) {
            qqBtn.setText(R.string.unbind);
            qqBtn.setBackgroundResource(R.drawable.unbind);
        } else {
            qqBtn.setText(R.string.bind);
            qqBtn.setBackgroundResource(R.drawable.bind);
        }
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener moreClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_bind_account_sina:
                        if (UMInfoAgent.isOauthed(mContext, SHARE_MEDIA.SINA)) {
                            mController.deleteOauth(mContext, SHARE_MEDIA.SINA,
                                    new SocializeClientListener() {
                                        @Override
                                        public void onStart() {
                                            showShortToast("解除绑定中...");
                                        }

                                        @Override
                                        public void onComplete(int status, SocializeEntity entity) {
                                            showShortToast("解除绑定成功");
                                            sinaBtn.setText(R.string.bind);
                                            sinaBtn.setBackgroundResource(R.drawable.bind);
                                        }
                                    });
                        } else {
                            try {
                                mController.doOauthVerify(mContext, SHARE_MEDIA.SINA,
                                        new UMAuthListener() {
                                            @Override
                                            public void onError(SocializeException e,
                                                    SHARE_MEDIA platform) {
                                            }

                                            @Override
                                            public void onComplete(Bundle value,
                                                    SHARE_MEDIA platform) {
                                                if (value != null
                                                        && !TextUtils.isEmpty(value
                                                                .getString("uid"))) {
                                                    // 获取用户信息
                                                    getSinaUserInfo();
                                                    // 改变状态
                                                    sinaBtn.setText(R.string.unbind);
                                                    sinaBtn.setBackgroundResource(R.drawable.unbind);
                                                    Toast.makeText(mContext, "授权成功.",
                                                            Toast.LENGTH_SHORT).show();
                                                } else {
                                                    Toast.makeText(mContext, "授权失败",
                                                            Toast.LENGTH_SHORT).show();
                                                }
                                            }

                                            @Override
                                            public void onCancel(SHARE_MEDIA arg0) {
                                            }

                                            @Override
                                            public void onStart(SHARE_MEDIA arg0) {
                                            }

                                        });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                        break;
                    case R.id.btn_bind_account_qq:
                        if (UMInfoAgent.isOauthed(mContext, SHARE_MEDIA.TENCENT)) {
                            mController.deleteOauth(mContext, SHARE_MEDIA.TENCENT,
                                    new SocializeClientListener() {
                                        @Override
                                        public void onStart() {
                                            showShortToast("解除绑定中...");
                                        }

                                        @Override
                                        public void onComplete(int status, SocializeEntity entity) {
                                            showShortToast("解除绑定成功");
                                            qqBtn.setText(R.string.bind);
                                            qqBtn.setBackgroundResource(R.drawable.bind);
                                        }
                                    });
                        } else {
                            try {
                                mController.doOauthVerify(mContext, SHARE_MEDIA.TENCENT,
                                        new UMAuthListener() {
                                            @Override
                                            public void onError(SocializeException e,
                                                    SHARE_MEDIA platform) {
                                            }

                                            @Override
                                            public void onComplete(Bundle value,
                                                    SHARE_MEDIA platform) {
                                                if (value != null
                                                        && !TextUtils.isEmpty(value
                                                                .getString("uid"))) {
                                                    // 获取用户信息
                                                    getTencentUserInfo();
                                                    qqBtn.setText(R.string.unbind);
                                                    qqBtn.setBackgroundResource(R.drawable.unbind);
                                                    Toast.makeText(mContext, "授权成功.",
                                                            Toast.LENGTH_SHORT).show();
                                                } else {
                                                    Toast.makeText(mContext, "授权失败",
                                                            Toast.LENGTH_SHORT).show();
                                                }
                                            }

                                            @Override
                                            public void onCancel(SHARE_MEDIA arg0) {
                                            }

                                            @Override
                                            public void onStart(SHARE_MEDIA arg0) {
                                            }

                                        });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        break;

                    default:
                        break;
                }
            }
        };
        this.sinaBtn.setOnClickListener(moreClick);
        this.qqBtn.setOnClickListener(moreClick);
    }

    /**
     * 获取新浪用户信息
     */
    void getSinaUserInfo() {
        mController.getPlatformInfo(mContext, SHARE_MEDIA.SINA, new UMDataListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(int status, Map<String, Object> info) {
                if (status == 200 && info != null) {
                    // StringBuilder sb = new StringBuilder();
                    // Set<String> keys = info.keySet();
                    // for (String kStr : keys) {
                    // sb.append(kStr + "=" + info.get(kStr).toString() +
                    // "\r\n");
                    // }
                    // android.util.Log.d("TestData", sb.toString());
                    // 获取用户昵称
                    WeiBoUserEntity userInfo = new WeiBoUserEntity();
                    String screen_name = info.get("screen_name").toString();
                    userInfo.setUserName(screen_name);
                    TokenStore.storeUserInfo(mContext, userInfo, ShareConfig.SHARE_WEIBO_SINA);
                    // 显示授权的用户昵称
                    sinaTv.setText(screen_name);
                }
            }
        });
    }

    /**
     * 获取腾讯用户信息
     */
    void getTencentUserInfo() {
        mController.getPlatformInfo(mContext, SHARE_MEDIA.TENCENT, new UMDataListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(int status, Map<String, Object> info) {
                if (status == 200 && info != null) {
                    // 获取用户昵称
                    WeiBoUserEntity userInfo = new WeiBoUserEntity();
                    String screen_name = info.get("screen_name").toString();
                    userInfo.setUserName(screen_name);
                    TokenStore.storeUserInfo(mContext, userInfo, ShareConfig.SHARE_WEIBO_QQ);
                    // 显示授权的用户昵称
                    qqTv.setText(screen_name);
                }
            }
        });
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        Intent intent = getIntent();
    }
}
